<template>
    <div class="app-container">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>起草公文</span>
        </div>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入公文标题"></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择公文类型">
              <el-option label="通知" value="notice"></el-option>
              <el-option label="请示" value="request"></el-option>
              <el-option label="报告" value="report"></el-option>
              <el-option label="决定" value="decision"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="紧急程度" prop="urgency">
            <el-radio-group v-model="form.urgency">
              <el-radio label="normal">普通</el-radio>
              <el-radio label="urgent">紧急</el-radio>
              <el-radio label="very_urgent">特急</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <el-input type="textarea" :rows="10" v-model="form.content" placeholder="请输入公文内容"></el-input>
          </el-form-item>
          <el-form-item label="附件">
            <el-upload
              class="upload-demo"
              action="https://jsonplaceholder.typicode.com/posts/"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :before-remove="beforeRemove"
              multiple
              :limit="3"
              :on-exceed="handleExceed"
              :file-list="fileList">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png/pdf文件，且不超过10MB</div>
            </el-upload>
          </el-form-item>
          <el-form-item label="审批流程" prop="approvers">
            <el-select
              v-model="form.approvers"
              multiple
              filterable
              remote
              reserve-keyword
              placeholder="请输入关键词"
              :remote-method="remoteMethod"
              :loading="loading">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
            <el-button @click="onSaveDraft">保存草稿</el-button>
            <el-button @click="onCancel">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script>
  export default {
    name: 'CreateDocument',
    data() {
      return {
        form: {
          title: '',
          type: '',
          urgency: 'normal',
          content: '',
          approvers: []
        },
        rules: {
          title: [
            { required: true, message: '请输入公文标题', trigger: 'blur' },
            { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请选择公文类型', trigger: 'change' }
          ],
          content: [
            { required: true, message: '请输入公文内容', trigger: 'blur' }
          ],
          approvers: [
            { required: true, message: '请选择审批人', trigger: 'change' }
          ]
        },
        fileList: [],
        options: [],
        loading: false
      }
    },
    created() {
      this.getApprovers()
    },
    methods: {
      getApprovers() {
        // 模拟获取审批人列表
        this.options = [
          { value: '1', label: '张三 (总经理)' },
          { value: '2', label: '李四 (副总经理)' },
          { value: '3', label: '王五 (人事部经理)' },
          { value: '4', label: '赵六 (财务部经理)' }
        ]
      },
      remoteMethod(query) {
        if (query !== '') {
          this.loading = true
          setTimeout(() => {
            this.loading = false
            this.options = this.options.filter(item => {
              return item.label.toLowerCase().includes(query.toLowerCase())
            })
          }, 200)
        } else {
          this.getApprovers()
        }
      },
      handleRemove(file, fileList) {
        console.log(file, fileList)
      },
      handlePreview(file) {
        console.log(file)
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`)
      },
      onSubmit() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$message({
              message: '提交成功',
              type: 'success'
            })
            // 实际项目中应该调用API
            // createDocument(this.form).then(() => {
            //   this.$message({
            //     message: '提交成功',
            //     type: 'success'
            //   })
            //   this.$router.push('/document/list')
            // })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      onSaveDraft() {
        this.$message({
          message: '草稿保存成功',
          type: 'success'
        })
        // 实际项目中应该调用API
        // saveDraft(this.form).then(() => {
        //   this.$message({
        //     message: '草稿保存成功',
        //     type: 'success'
        //   })
        //   this.$router.push('/document/list')
        // })
      },
      onCancel() {
        this.$router.push('/document/list')
      }
    }
  }
  </script>